package kr.ac.hufs;

import java.awt.TextArea;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class requestMsgExecuteFunc {
	
	public utilFunc utilFunc = new utilFunc();
	String IP="";
	public Socket connectFTPServ(UserInfoProvider uip,  int portN) throws UnknownHostException,
			IOException {
		
		Socket socket = null;

		socket = new Socket(uip.getIPAddr(), portN);
		IP = uip.getIPAddr();
		return socket;
	}
	
	public Socket connectFTPDataServ(StringBuffer DATA_PORT_NUM,UserInfoProvider uip) throws IOException {

		if (var.ACTIVE_MODE == var.OFF) {
			int portN;
			Socket socket2 = null;
			String temp=null;
			temp = DATA_PORT_NUM.toString();		
			portN=Integer.parseInt(temp.trim());
			socket2 = new Socket(uip.getIPAddr(),portN);		
			return socket2;

		} else if (var.ACTIVE_MODE == var.ON) {
			//ACTIVE_PORT_NUM++;
			return utilFunc.listenFTPServConn();
		}
		return null;
	}
		
	
	public void execCommand(int requestNum,StringBuffer command,Socket socket,UserInfoProvider uip,TextArea textArea) throws IOException {
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(uip.getSocket().getOutputStream()));
		String temp=null;
		String tmp;

		if(requestNum == var.OPEN) {
			//Scanner c = new Scanner(System.in);
			//tmp = c.nextLine();
			System.out.println("dddd"+uip.getID());
			command.append("USER ");
			//command.append(tmp+"\r\n");
			command.append(uip.getID()+"\r\n");

		}
		else if(requestNum == var.PASSWORD) {
			//Scanner c = new Scanner(System.in);
			//tmp = c.nextLine();
			command.append("PASS ");
			command.append(uip.getPWD()+"\r\n");
		}
		else if(requestNum == var.PWD) {
			//Scanner c = new Scanner(System.in);
			//tmp = c.nextLine();
			command.append("PWD\r\n");			
		}
		else if(requestNum == var.LS)
		{
			command.append("LIST\r\n");
		}		
		else if(requestNum == var.PUT)
		{
			command.append("STOR "+uip.getCli_fileName()+"\r\n");
		}
		else if(requestNum == var.GET)
		{
			command.append("RETR "+uip.getSrv_filename()+"\r\n");
		}
		else if(requestNum == var.CD)
		{
			System.err.println("adsfasdf"+uip.getCdName());
			command.append("CWD "+uip.getCdName()+"\r\n");			
		}
		temp = command.toString();
		utilFunc.debug(command,textArea);
		bw.write(temp);
		bw.flush();
		command.delete(0, 100);
	}
	
	public Socket setServerConnMode(Socket socket,TextArea textArea,UserInfoProvider uip) throws IOException {
		StringBuffer tmp = new StringBuffer();
		StringBuffer buf = new StringBuffer();
		StringBuffer activeMsg = new StringBuffer();
	
		String temp2;
		var.DATA_CONN_MODE = var.ON;
		System.out.println(socket);
		socket = uip.getSocket();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
		if (var.ACTIVE_MODE == var.OFF) {
			tmp.append("PASV\r\n");
			utilFunc.debug(tmp,textArea);
			bw.write("PASV\r\n");
			bw.flush();
			
		    return socket;
		} else if (var.ACTIVE_MODE == var.ON) {
			utilFunc.makeActiveMsg(utilFunc.getMyAddr(socket),activeMsg);
			
			
			utilFunc.debug(activeMsg,textArea);
			temp2 = activeMsg.toString();
			bw.write(temp2);
			bw.flush();	
			return socket;
		}
		return socket;
	}
	
	public void setServerAsciiMode(Socket socket,TextArea textArea) throws IOException
	{
		StringBuffer tmp= new StringBuffer();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
		tmp.append("TYPE A\r\n");
		utilFunc.debug(tmp,textArea);
		if(var.BINARY_MODE == var.ON)
		{
		bw.write("TYPE I\r\n");
		bw.flush();
		}
		else
		{
			bw.write("TYPE A\r\n");
			bw.flush();
		}
	}
	
	public void setServerBinaryMode(Socket socket) throws IOException {
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
		bw.write("TYPE I\r\n");
		bw.flush();
	}
}
